﻿package com.tr1701_demo.controller;

import java.io.IOException;    
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
import javax.servlet.http.HttpSession;    
import java.util.*;    
import java.awt.*;    
import java.awt.image.*;    
    
import javax.imageio.*;    
/** 
 *  
 * @author mzba 
 * 
 */  
@WebServlet("/getcaptcha")
public class GetCaptcha extends HttpServlet {    
    private static final long serialVersionUID = 1L;    
    
    /**  
     * @see HttpServlet#HttpServlet()  
     */    
    public GetCaptcha() {    
        super();    
    }    
    
    /**  
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse  
     *      response)  
     */    
    protected void doGet(HttpServletRequest request,    
            HttpServletResponse response) throws ServletException, IOException {    
        /*response.setContentType("image/jpeg");    
        response.setHeader("Pragma", "No-cache");    
        response.setHeader("Cache-Control", "no-cache");    
        response.setDateHeader("Expires", 0);    
        HttpSession session = request.getSession();    
        //????????????????????
        BufferedImage bi=new BufferedImage(68, 22,BufferedImage.TYPE_INT_RGB);//???????????
        Graphics g=bi.getGraphics(); //????????????????????
        Color c=new Color(100,150,255); //???????
        //?????????????????ο?  
        g.setColor(c);//????????????????
        g.fillRect(0, 0, 68,22); //fillRect:???????????

        char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();//??????????????
        Random r=new Random();
        int len=ch.length;
        int index; //index?????????????
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<4;i++){
           index=r.nextInt(len);//???????????
           g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));  //???????
           g.drawString(ch[index]+"",(i*15)+3, 18);//??????????????λ??
           sb.append(ch[index]);
        }
        request.getSession().setAttribute("piccode",sb.toString()); //???????????session?У?????????????
        
        ImageIO.write(bi, "JPG", response.getOutputStream()); */    
    	response.setContentType("image/jpeg");    
        response.setHeader("Pragma", "No-cache");    
        response.setHeader("Cache-Control", "no-cache");    
        response.setDateHeader("Expires", 0);    
        HttpSession session = request.getSession();    
        int width = 75, height = 25;    
        BufferedImage image = new BufferedImage(width, height,    
                BufferedImage.TYPE_INT_RGB);    
        Graphics g = image.getGraphics();    
        Random random = new Random();    
        g.setColor(getRandColor(200, 250));    
        g.fillRect(0, 0, width, height);    
        g.setFont(new Font("Times New Roman", Font.PLAIN, 24));    
        g.setColor(getRandColor(160, 200));    
        g.drawRect(0, 0, width - 1, height - 1);    
        g.setColor(getRandColor(160, 200));    
        for (int i = 0; i < 155; i++) {    
            int x = random.nextInt(width);    
            int y = random.nextInt(height);    
            int xl = random.nextInt(12);    
            int yl = random.nextInt(12);    
            g.drawLine(x, y, x + xl, y + yl);    
        }    
        String sRand = "";    
        for (int i = 0; i < 4; i++) {    
            String rand = String.valueOf(random.nextInt(10));    
            sRand += rand;    
            g.setColor(new Color(20 + random.nextInt(110), 20 + random    
                    .nextInt(110), 20 + random.nextInt(110)));    
            g.drawString(rand, 13 * i + 14, 20);    
        }    
        //将生成的图形验证码存储到Session中
        session.setAttribute("piccode", sRand);    
    
        g.dispose();    
        //将图片写到页面中
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }    
    
    /**  
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse  
     *      response)  
     */    
    protected void doPost(HttpServletRequest request,    
            HttpServletResponse response) throws ServletException, IOException {    
        // TODO Auto-generated method stub    
    }    
    
    Color getRandColor(int fc, int bc) {// ??????Χ?????????    
        Random random = new Random();    
        if (fc > 255)    
            fc = 255;    
        if (bc > 255)    
            bc = 255;    
        int r = fc + random.nextInt(bc - fc);    
        int g = fc + random.nextInt(bc - fc);    
        int b = fc + random.nextInt(bc - fc);    
        return new Color(r, g, b);    
    }    
    
}   